package com.foodplus.www.view.fragments;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.foodplus.www.adapter.shop.ShopAdapter;
import com.foodplus.www.api.ExceptionHandle;
import com.foodplus.www.api.ParamManager;
import com.foodplus.www.bean.shop.BannerBean;
import com.foodplus.www.bean.shop.ChannelBean;
import com.foodplus.www.bean.shop.QueryBean;
import com.foodplus.www.bean.shop.ShopBean;
import com.foodplus.www.bean.shop.TagClassifyBean;
import com.foodplus.www.foodplus.R;
import com.foodplus.www.presenter.ShopPresenter;
import com.foodplus.www.view.activity.GoodActivity;
import com.foodplus.www.view.activity.SipGoodActivity;
import com.foodplus.www.view.activity.TagActivity;
import com.foodplus.www.view.impl.ShopView;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.github.jdsjlzx.recyclerview.ProgressStyle;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShopFragment extends Fragment implements ShopView,View.OnClickListener {

    @BindView(R.id.shop_recycler)
    LRecyclerView recyclerView;

    //数据容器
    private List<BannerBean> bannerBeanList;
    private List<String> bannerUrlList;
    private List<TagClassifyBean> tagClassifyBeanList;
    private ChannelBean channelBean;
    private List<QueryBean> list;
    //适配器
    private ShopAdapter adapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    //p层数据源
    private ShopPresenter shopPresenter;
    //banner头
    private RelativeLayout bannerRl;
    private ImageView bannerParIv;
    private Banner banner;
    //tag头
    private LinearLayout tag;
    private LinearLayout tag01;
    private LinearLayout tag02;
    private LinearLayout tag03;
    private LinearLayout tag04;
    private LinearLayout tag05;
    private LinearLayout tag06;
    private LinearLayout tag07;
    private LinearLayout tag08;
    private TextView tagTv01;
    private TextView tagTv02;
    private TextView tagTv03;
    private TextView tagTv04;
    private TextView tagTv05;
    private TextView tagTv06;
    private TextView tagTv07;
    private TextView tagTv08;
    private ImageView tagIv01;
    private ImageView tagIv02;
    private ImageView tagIv03;
    private ImageView tagIv04;
    private ImageView tagIv05;
    private ImageView tagIv06;
    private ImageView tagIv07;
    private ImageView tagIv08;
    //channel头
    private RelativeLayout channel;
    private ImageView channelIv;
    private TextView tv1;
    private TextView price;
    private TextView oldprice;
    private TextView tv2;


    public ShopFragment() {
    }

    public static ShopFragment newInstance() {
        Bundle args = new Bundle();
        ShopFragment fragment = new ShopFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop, container, false);
        ButterKnife.bind(this, view);
        //初始化容器
        bannerBeanList = new ArrayList<>();
        tagClassifyBeanList = new ArrayList<>();
        list = new ArrayList<>();
        bannerUrlList = new ArrayList<>();
        //绘制UI
        initView();

        //获得数据
        getData();
        return view;
    }

    private void initView() {
        //RecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        adapter = new ShopAdapter(list, getActivity());
        setRecyclerItemClick();

        //加入query
        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        //下拉刷新
        recyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        recyclerView.setArrowImageView(R.mipmap.ic_refreshing);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                shopPresenter.getShopData(ParamManager.getInstance().getShopParams());
                banner.setVisibility(View.INVISIBLE);
                channel.setVisibility(View.INVISIBLE);
                recyclerView.refreshComplete();
            }
        });
        addHeaderBanner();
        addHeaderTag();
        addHeaderChannel();
    }

    private void setRecyclerItemClick() {
        //点击事件
        adapter.setOnItemClickListener(new ShopAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(int position) {
                Intent intent = new Intent(getActivity(), GoodActivity.class);
                intent.putExtra("id", list.get(position-4).getSpecial_id());
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(R.anim.shop_trans_enter, 0);
            }
        });
    }

    private void getData() {
        shopPresenter = new ShopPresenter(this);
        shopPresenter.getShopData(ParamManager.getInstance().getShopParams());
    }

    @Override
    public void onSuccess(ShopBean shopBean) {
        list.clear();
        bannerBeanList.clear();
        tagClassifyBeanList.clear();
        channelBean = null;
        //整理数据
        list.addAll(shopBean.getDatas().getQuery());
        bannerBeanList.addAll(shopBean.getDatas().getBanner());
        if (shopBean.getDatas().getBanner() != null) {
            for (int i = 0; i < shopBean.getDatas().getBanner().size(); i++) {
                String urlStr = shopBean.getDatas().getBanner().get(i).getAdvertImg();
                bannerUrlList.add(urlStr);
            }
        }
        tagClassifyBeanList.addAll(shopBean.getDatas().getTag_classify());
        channelBean = shopBean.getDatas().getChannel();
        //刷新数据
        //recycler数据
        lRecyclerViewAdapter.notifyDataSetChanged();
        //banner数据
        loadBannerData();
        //tag数据
        loadTagData();
        //channel数据
        loadChannelData();

    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {
        Log.e("ShopFragment", "Getting Data Failed At ShopFrag");
    }

    @Override
    public void onFinish() {

    }

    private void addHeaderBanner() {
        //banner
        bannerRl = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.fragment_shop_banner, null);
        RelativeLayout.LayoutParams paramsBanner = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        bannerRl.setLayoutParams(paramsBanner);
        bannerParIv = (ImageView) bannerRl.findViewById(R.id.shop_banner_iv);
        banner = (Banner) bannerRl.findViewById(R.id.shop_banner);
        banner.setVisibility(View.INVISIBLE);
        //加入头
        lRecyclerViewAdapter.addHeaderView(bannerRl);
    }

    private void addHeaderChannel() {
        //channel
        channel = (RelativeLayout) LayoutInflater.from(getContext()).inflate(R.layout.fragment_shop_channel, null);
        RelativeLayout.LayoutParams paramsChannel = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        channel.setLayoutParams(paramsChannel);
        channelIv = (ImageView) channel.findViewById(R.id.shop_channel_iv);
        tv1 = (TextView) channel.findViewById(R.id.shop_channel_tv1);
        price = (TextView) channel.findViewById(R.id.shop_channel_price);
        oldprice = (TextView) channel.findViewById(R.id.shop_channel_oldprice);
        tv2 = (TextView) channel.findViewById(R.id.shop_channel_tv2);
        channel.setVisibility(View.INVISIBLE);
        //加入头
        lRecyclerViewAdapter.addHeaderView(channel);
    }

    private void addHeaderTag() {
        //channel
        tag = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.fragment_shop_tag, null);
        LinearLayout.LayoutParams paramsTag = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
        tag.setLayoutParams(paramsTag);
        tagTv01 = (TextView) tag.findViewById(R.id.shop_tag_tv01);
        tagTv02 = (TextView) tag.findViewById(R.id.shop_tag_tv02);
        tagTv03 = (TextView) tag.findViewById(R.id.shop_tag_tv03);
        tagTv04 = (TextView) tag.findViewById(R.id.shop_tag_tv04);
        tagTv05 = (TextView) tag.findViewById(R.id.shop_tag_tv05);
        tagTv06 = (TextView) tag.findViewById(R.id.shop_tag_tv06);
        tagTv07 = (TextView) tag.findViewById(R.id.shop_tag_tv07);
        tagTv08 = (TextView) tag.findViewById(R.id.shop_tag_tv08);
        tagIv01 = (ImageView) tag.findViewById(R.id.shop_tag_iv01);
        tagIv02 = (ImageView) tag.findViewById(R.id.shop_tag_iv02);
        tagIv03 = (ImageView) tag.findViewById(R.id.shop_tag_iv03);
        tagIv04 = (ImageView) tag.findViewById(R.id.shop_tag_iv04);
        tagIv05 = (ImageView) tag.findViewById(R.id.shop_tag_iv05);
        tagIv06 = (ImageView) tag.findViewById(R.id.shop_tag_iv06);
        tagIv07 = (ImageView) tag.findViewById(R.id.shop_tag_iv07);
        tagIv08 = (ImageView) tag.findViewById(R.id.shop_tag_iv08);
        tag01 = (LinearLayout) tag.findViewById(R.id.shop_tag_01);
        tag02 = (LinearLayout) tag.findViewById(R.id.shop_tag_02);
        tag03 = (LinearLayout) tag.findViewById(R.id.shop_tag_03);
        tag04 = (LinearLayout) tag.findViewById(R.id.shop_tag_04);
        tag05 = (LinearLayout) tag.findViewById(R.id.shop_tag_05);
        tag06 = (LinearLayout) tag.findViewById(R.id.shop_tag_06);
        tag07 = (LinearLayout) tag.findViewById(R.id.shop_tag_07);
        tag08 = (LinearLayout) tag.findViewById(R.id.shop_tag_08);
        tag01.setOnClickListener(this);
        tag02.setOnClickListener(this);
        tag03.setOnClickListener(this);
        tag04.setOnClickListener(this);
        tag05.setOnClickListener(this);
        tag06.setOnClickListener(this);
        tag07.setOnClickListener(this);
        tag08.setOnClickListener(this);
        //加入头
        lRecyclerViewAdapter.addHeaderView(tag);
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.shop_tag_01:
                Intent intent1 = new Intent(getActivity(), TagActivity.class);
                intent1.putExtra("id", tagClassifyBeanList.get(0).getTag_id());
                intent1.putExtra("title", tagClassifyBeanList.get(0).getTag_name());
                getActivity().startActivity(intent1);
                getActivity().overridePendingTransition(R.anim.shop_trans_enter, 0);
                break;
            case R.id.shop_tag_02:
                Intent intent2 = new Intent(getActivity(), TagActivity.class);
                intent2.putExtra("id", tagClassifyBeanList.get(1).getTag_id());
                intent2.putExtra("title", tagClassifyBeanList.get(1).getTag_name());
                getActivity().startActivity(intent2);
                getActivity().overridePendingTransition(R.anim.shop_trans_enter, 0);
                break;
            case R.id.shop_tag_03:
                Intent intent3 = new Intent(getActivity(), TagActivity.class);
                intent3.putExtra("id", tagClassifyBeanList.get(2).getTag_id());
                intent3.putExtra("title", tagClassifyBeanList.get(2).getTag_name());
                getActivity().startActivity(intent3);
                getActivity().overridePendingTransition(R.anim.shop_trans_enter, 0);
                break;
            case R.id.shop_tag_04:
                Intent intent4 = new Intent(getActivity(), TagActivity.class);
                intent4.putExtra("id", tagClassifyBeanList.get(3).getTag_id());
                intent4.putExtra("title", tagClassifyBeanList.get(3).getTag_name());
                getActivity().startActivity(intent4);
                getActivity().overridePendingTransition(R.anim.shop_trans_enter, 0);
                break;
            case R.id.shop_tag_05:
                Intent intent5 = new Intent(getActivity(), TagActivity.class);
                intent5.putExtra("id", tagClassifyBeanList.get(4).getTag_id());
                intent5.putExtra("title", tagClassifyBeanList.get(4).getTag_name());
                getActivity().startActivity(intent5);
                getActivity().overridePendingTransition(R.anim.shop_trans_enter, 0);
                break;
            case R.id.shop_tag_06:
                Intent intent6 = new Intent(getActivity(), TagActivity.class);
                intent6.putExtra("id", tagClassifyBeanList.get(5).getTag_id());
                intent6.putExtra("title", tagClassifyBeanList.get(5).getTag_name());
                getActivity().startActivity(intent6);
                getActivity().overridePendingTransition(R.anim.shop_trans_enter, 0);
                break;
            case R.id.shop_tag_07:
                Intent intent7 = new Intent(getActivity(), TagActivity.class);
                intent7.putExtra("id", tagClassifyBeanList.get(6).getTag_id());
                intent7.putExtra("title", tagClassifyBeanList.get(6).getTag_name());
                getActivity().startActivity(intent7);
                getActivity().overridePendingTransition(R.anim.shop_trans_enter, 0);
                break;
            case R.id.shop_tag_08:
                Intent intent8 = new Intent(getActivity(), TagActivity.class);
                intent8.putExtra("id", tagClassifyBeanList.get(7).getTag_id());
                intent8.putExtra("title", tagClassifyBeanList.get(7).getTag_name());
                getActivity().startActivity(intent8);
                getActivity().overridePendingTransition(R.anim.shop_trans_enter, 0);
                break;

        }
    }
    private void loadChannelData() {
        channel.setVisibility(View.VISIBLE);
        Glide.with(getContext()).load(channelBean.getGoods_img()).placeholder(R.mipmap.bg_morentu).into(channelIv);
        tv1.setText(channelBean.getTitle());
        price.setText("¥" + channelBean.getZhekou());
        oldprice.setText("¥" + channelBean.getGoods_price());
        tv2.setText(channelBean.getGoods_name());
    }

    private void loadTagData() {
        tagTv01.setText(tagClassifyBeanList.get(0).getTag_name());
        Glide.with(getContext()).load(tagClassifyBeanList.get(0).getTag_img()).into(tagIv01);
        tagTv02.setText(tagClassifyBeanList.get(1).getTag_name());
        Glide.with(getContext()).load(tagClassifyBeanList.get(1).getTag_img()).into(tagIv02);
        tagTv03.setText(tagClassifyBeanList.get(2).getTag_name());
        Glide.with(getContext()).load(tagClassifyBeanList.get(2).getTag_img()).into(tagIv03);
        tagTv04.setText(tagClassifyBeanList.get(3).getTag_name());
        Glide.with(getContext()).load(tagClassifyBeanList.get(3).getTag_img()).into(tagIv04);
        tagTv05.setText(tagClassifyBeanList.get(4).getTag_name());
        Glide.with(getContext()).load(tagClassifyBeanList.get(4).getTag_img()).into(tagIv05);
        tagTv06.setText(tagClassifyBeanList.get(5).getTag_name());
        Glide.with(getContext()).load(tagClassifyBeanList.get(5).getTag_img()).into(tagIv06);
        tagTv07.setText(tagClassifyBeanList.get(6).getTag_name());
        Glide.with(getContext()).load(tagClassifyBeanList.get(6).getTag_img()).into(tagIv07);
        tagTv08.setText(tagClassifyBeanList.get(7).getTag_name());
        Glide.with(getContext()).load(tagClassifyBeanList.get(7).getTag_img()).into(tagIv08);
    }

    private void loadBannerData() {
        bannerParIv.setVisibility(View.INVISIBLE);
        banner.setVisibility(View.VISIBLE);
        banner.setImages(bannerUrlList);
        banner.setImageLoader(new MyImageLoad());
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.isAutoPlay(true);
        banner.setDelayTime(4000);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent = new Intent(getActivity(), SipGoodActivity.class);
                String[] strs = bannerBeanList.get(position-1).getAdvertUrl().split("\"");
                intent.putExtra("id", strs[7]);
                getActivity().startActivity(intent);
                getActivity().overridePendingTransition(R.anim.shop_trans_enter, 0);
            }
        });
        banner.start();
    }



    public class MyImageLoad implements ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).placeholder(R.mipmap.bg_morentu).fitCenter().into(imageView);
        }
    }
}
